from drf_haystack.serializers import HaystackSerializer
from rest_framework import serializers

from apps.leadnews.models import Channel, Article, Comment
from apps.leadnews.search_indexes import ArticleIndex
from apps.user.models import User


class ChannelSerializer(serializers.ModelSerializer):
    """频道序列化器"""

    class Meta:
        model = Channel
        fields = '__all__'


# class UserOptine111111111Serializer(serializers.ModelSerializer):
#     class Meta:
#         model = User
#         fields = ('id', 'username', 'avatar')

# class CommentOpt11111ineSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Comment
#         fields = ('id', 'user', 'content', "article")


class ArticleSerializer(serializers.ModelSerializer):
    """文章序列化器"""

    # user = UserOptineSerializer()

    class Meta:
        model = Article
        fields = ("id", "title")  # "labels",
        # fields = '__all__'


class UserOptineSerializer(serializers.ModelSerializer):
    articles = ArticleSerializer(many=True, read_only=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'avatar', 'articles', 'fans')


class SubsSerializer(serializers.ModelSerializer):
    # user = UserOptineSerializer(read_only=True)
    # subs = serializers.StringRelatedField(read_only=True)

    class Meta:
        model = Comment
        fields = ('id', 'content', 'article', 'user', 'parent', 'subs', 'createtime')
        depth = 3


class CommentSubsSerializer(serializers.ModelSerializer):
    subs = SubsSerializer(many=True, read_only=True)
    user = UserOptineSerializer(read_only=True)

    class Meta:
        model = Comment
        fields = ('content', 'id', 'user', 'subs', 'createtime', 'updatetime', 'parent', 'article')
        # depth = 3


class CommentDetailSerializer(serializers.ModelSerializer):
    user = UserOptineSerializer(read_only=True)
    comments = CommentSubsSerializer(many=True, read_only=True)
    collected_users = serializers.StringRelatedField(many=True, read_only=True)

    class Meta:
        model = Article
        fields = (
            'id', 'user', 'comments', 'title', 'content', 'image', 'createtime', 'updatetime', 'visits', 'thumbup',
            'comment_count', 'channel', 'labels', 'collected_users')


# 获取某个频道之下的文章列表
class ChannelDetailSerializer(serializers.ModelSerializer):
    user = UserOptineSerializer(read_only=True)

    # collected = serializers.BooleanField()
    class Meta:
        model = Article
        fields = ('id', 'title', 'content', 'createtime', 'user', 'collected_users', 'image')


class PutArticleSerializer(serializers.ModelSerializer):
    """发表文章的序列化器"""

    class Meta:
        model = Article
        fields = ("content", "title", "channel", "image", "labels", "user")  # "labels",

    def create(self, validated_data):
        labels = validated_data.pop('labels')
        article = Article.objects.create(**validated_data)
        article.labels.add(*labels)
        return article


class ArticleCreateSerializer(serializers.ModelSerializer):
    """评论"""

    class Meta:
        model = Comment
        fields = '__all__'
        # fields = ("content",'parent','article','user')

    def create(self, validated_data):
        if len(validated_data) == 3:
            data = {
                "content": validated_data["content"],
                "article_id": validated_data["article"].id,
                "user_id": validated_data["article"].user_id,
                "parent_id": validated_data['parent'].id
            }
        else:
            data = {
                "content": validated_data["content"],
                "article_id": validated_data["article"].id,
                "user_id": validated_data["article"].user_id,
                # "parent_id": validated_data['parent'].id
            }

        # article = Article.objects.create(**validated_data)
        article = Comment.objects.create(**data)
        return article


class CollectArticleSerializer(serializers.ModelSerializer):
    """收藏文章"""

    class Meta:
        model = Article
        fields = '__all__'


# 搜索

class ArticleSerializers(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ['id', 'createtime', 'content', 'title']


class ArticleIndexSerializer(HaystackSerializer):
    """
    Article索引结果数据序列化器
    """
    object = ArticleSerializers()

    class Meta:
        index_classes = [ArticleIndex]
        fields = ('object', 'text')
